package com.wondersgroup.framework.sample.component.form.action;

import com.wondersgroup.framework.core.bo.Page;
import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.component.form.vo.GridSelectVO;
import com.wondersgroup.framework.sample.component.grid.vo.GridVO;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class GridSelectAction extends BaseAjaxAction
{
  private GridSelectVO gridSelectVO;
  private static final String prefix = "{\"success\":true,result:[";
  private static final String suffex = "]}";

  public GridSelectAction()
  {
    this.gridSelectVO = new GridSelectVO();
  }

  public ValueObject getValueObject()
  {
    return this.gridSelectVO;
  }

  public GridSelectVO getGridSelectVO() {
    return this.gridSelectVO;
  }

  public void setGridSelectVO(GridSelectVO gridSelectVO) {
    this.gridSelectVO = gridSelectVO;
  }

  public String loadGridSelectData()
  {
    String[] ids = new String[33];
    for (int i = 0; i < 33; ++i) {
      ids[i] = String.valueOf(i+1);
    }

    String[] names = { "aicheng(艾诚)", "chenweiwen(陈伟雯)", "chenjialong(陈佳龙)", "dingji(丁继)", "gaohaitao(高海涛)", "guojingnan(郭京楠)", "hehua(何华)", "huangjieyun(黄杰昀)", "jiazhao(贾兆)", "jiwulong(纪物龙)", "liangjingjie(梁婧杰)", "like(李可)", "lidongsheng(李冬圣)", "liuming(刘鸣)", "liuxing(刘兴)", "linengfeng(李能峰)", "luzhongzhi(路忠治)", "panpu(潘谱)", "shangcheng(商成)", "shening(佘宁)", "tongjin(童金)", "wangke(王科)", "wangying(王英)", "wangyushu(王玉书)", "xiaoqiuxiang(肖秋翔)", "yaolitao(姚黎涛)", "yejunhua(叶俊华)", "zhangjian(张剑)", "zhangli(张立)", "zhangzhenlong(张振龙)", "zhaoyufei(赵宇飞)", "zhouchao(周超)", "zhaoxiao(周小)" };

    String queryText = this.servletRequest.getParameter("query");

    int start = 0;
    int size = 10;
    String pStart = this.servletRequest.getParameter("start");
    String pSize = this.servletRequest.getParameter("limit");
    if (pStart != null)
      start = Integer.parseInt(pStart);

    if (pSize != null) {
      size = Integer.parseInt(pSize);
    }

    List result = new ArrayList();
    for (int i = 0; i < 33; ++i) {
      GridVO vo = new GridVO();
      vo.setId(ids[i]);
      vo.setName(names[i]);
      if ((queryText != null) && (((names[i].indexOf(queryText) > -1) || (queryText.length() == 0))))
        result.add(vo);

    }

    List finalData = new ArrayList();
    int end = (start + size < result.size()) ? start + size : result.size();
    for (int i = start; i < end; ++i) {
      finalData.add(result.get(i));
    }

    Page page = new Page(start, end - start, result.size(), size, finalData);
    createJSonData(VOUtils.getJsonDataFromPage(page, GridVO.class));

    return "ajax";
  }

  public String loadFormWithGridSelect()
  {
    GridSelectVO vo = new GridSelectVO();
    vo.setPeople("16");
    vo.setName("linengfeng(李能峰)");

    String json = "{\"success\":true,result:[" + VOUtils.getJsonData(vo) + "]}";
    createJSonData(json);
    return "ajax";
  }

  public String submitFormWithGridSelect()
  {
    System.out.println("人员编号：" + this.gridSelectVO.getPeople());
    return "ajax";
  }
}